package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.VerifyIdentityActivity;
import com.cljz.makepassenger.utils.L;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/5.
 */

public class VerifyIdentityP extends BaseP<VerifyIdentityActivity> {
    public void getCode(String phone) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("send_type", Constant.SmsType.RESET_PAYMENT_PASS);
        paramsMap.put("phone", "1" + phone);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getSmsSendData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean smsSendBean) {
                        if ("0".equals(smsSendBean.getError_code())) {
                            getV().getSmsSucceed();
                        } else if ("1000".equals(smsSendBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getSmsFail(smsSendBean.getMsg());
                        }

                    }
                });

    }

    public void checkResetCode(String phone, String code) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("phone", phone);
        paramsMap.put("verify_code", code);
        paramsMap.put("token", getToken());
        L.e(gson.toJson(paramsMap));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().checkResetCodeData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        L.e(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().checkResetCodeSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().checkResetCodeFail(responseBean.getMsg());
                        }
                    }
                });
    }

}
